<?php
/**
 * Created by PhpStorm.
 * User: hehecat
 * Date: 2018/8/29
 * Time: 20:00
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Session;
use app\admin\model\VideoModel;

class Video extends Controller {
    public function video(){
        $admin_id = 1;
        
        $model = new VideoModel();
        $result = $model->getVideo($admin_id);
        $this->assign("list",$result);
        return $this->fetch();
    }

    public function addVideo() {
        $admin_id = 1;

        $videoname = input("videoname");
        $intro = input("intro");
        $video = request()->file('video');

        $model = new VideoModel();
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($video){
            $info = $video->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
               $result = $model->addVideo($admin_id, $videoname,$info->getSaveName(),$intro);
               if ($result){
                   $this->success("添加成功");
               }
            }else{
                // 上传失败获取错误信息
                echo $video->getError();
            }
        }
    }

    public function getVIdeoSrc() {
        $videoid = input("videoid");
        $model = new VideoModel();
        $res = $model->getVIdeoSrc($videoid);
        return json($res);
    }

    public function deleteVideo() {
        $videoid = input("videoid");
        $model = new VideoModel();
        $res = $model->deleteVideo($videoid);
        return json($res);
    }

    public function editVideo() {
        $videoid = input("videoid");
        $videoname = input("videoname");
        $intro = input("intro");

        $video = request()->file('video');

        $model = new VideoModel();
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($video){
            $info = $video->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $result = $model->editVideo($videoid,$videoname,$info->getSaveName(),$intro);
                if ($result){
                    $this->success("修改成功");
                }
            }else{
                // 上传失败获取错误信息
                echo $video->getError();
            }
        }else {
            $result = $model->editVideo2($videoid,$videoname,$intro);
            if ($result){
                $this->success("修改成功");
            }
        }
    }

    public function test() {
        $club_id = Db::table('admin')->where('adminid',1)->find()['clubid'];
        $video = Db::table('video')->where('clubid',$club_id)->select();
        var_dump($video) ;
    }
}